package com.ct.boot.launch.service;

import com.ct.boot.launch.dao.testdb.ArticleRepository;
import com.ct.boot.launch.dao.testdb2.MessageRepository;
import com.ct.boot.launch.generator.Article;
import com.ct.boot.launch.generator.ArticleMapper;
import com.ct.boot.launch.model.ArticleVO;
import com.ct.boot.launch.utils.DozerUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: Charles chen
 * @Date: 2020/7/15 17:13
 * @Description:
 */
@Service
public class ArticleServiceJDBCImpl implements ArticleService {
//    @Autowired
//    ArticleRepository articleRepository;
//    @Autowired
//    MessageRepository messageRepository;
    @Resource
    ArticleMapper articleMapper;
    @Autowired
    private Mapper dozerMapper;

    @Override
    public String saveArticle(ArticleVO article) {
        Article articlePO = dozerMapper.map(article, Article.class);
        int insert = articleMapper.insert(articlePO);
        return insert + "";
    }

    @Override
    public void deleteArticle(Long id) {
        articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateArticle(ArticleVO article) throws Exception {
        Article articlePO = dozerMapper.map(article, Article.class);
        articleMapper.updateByPrimaryKeySelective(articlePO);//字段不为空才赋值 Example作为条件
        //Selective表示空值不更新

    }

    @Override
    public ArticleVO getArticle(Long id) {
        return dozerMapper.map(articleMapper.selectByPrimaryKey(id), ArticleVO.class);
    }

    @Override
    public List<ArticleVO> getAll() {
        List<Article> articles = articleMapper.selectByExample(null);//查询所有
        return DozerUtils.mapList(articles, ArticleVO.class);
    }
}
